<%@ page import="java.sql.*"%>
<%@ page import="java.util.*"%>
<%@ page import="org.jbpm.db.*"%>
<%@ page import="org.jpage.jbpm.context.*"%>
<%
    String sql = request.getParameter("sql");
	if(sql != null){
        Context.create();
		Connection con = null;
        PreparedStatement psmt = null;
		ResultSet rs = null;
		PersistenceContext persistenceContext = null;
		List rows = new ArrayList();
		try {
			persistenceContext = Context.getPersistenceContext();
			persistenceContext.beginTransaction();
			JbpmSession jbpmSession = Context.getPersistenceContext()
				.getJbpmSession();
			con = jbpmSession.getConnection();
            
			if(sql.trim().toLowerCase().startsWith("select")){
				System.out.println(sql);
				Statement stmt = con.createStatement();
			    rs = stmt.executeQuery(sql);
                ResultSetMetaData rsmd = rs.getMetaData();
				int count = rsmd.getColumnCount();
				Map columnMap = new LinkedHashMap();
				for (int i = 1; i <= count; i++) {
					String columnName = rsmd.getColumnName(i);
					columnMap.put(columnName, columnName);
				}
				while (rs.next()) {
					int index = 0;
					Map dataMap = new HashMap();
					Iterator iterator = columnMap.keySet().iterator();
					while (iterator.hasNext()) {
						index++;
						String name = (String) iterator.next();
						String value = rs.getString(index);
						dataMap.put(name.toLowerCase(), value);
					}
					rows.add(dataMap);
					out.println(dataMap);
					out.println("<br><br>");
				}
				rs.close();
				stmt.close();
			}else{
				if(sql.indexOf(";")>0){
					StringTokenizer token = new StringTokenizer(sql, ";");
					while(token.hasMoreTokens()){
						String tmp = token.nextToken();
						System.out.println(tmp);
						if(tmp.trim().length() == 0){
							continue;
						}
						psmt = con.prepareStatement(tmp);
				        psmt.executeUpdate();
				        psmt.close();
					    psmt = null;
					}
				}else{
					System.out.println(sql);
				    psmt = con.prepareStatement(sql);
				    psmt.executeUpdate();
				    psmt.close();
					psmt = null;
				}
			}
			out.println("<br><div align=center>Commond OK</div>");
		} catch (Exception ex) {
			ex.printStackTrace();
			throw new RuntimeException(ex.getMessage());
		} finally{
			try {
				persistenceContext.endTransaction();
			} catch (Exception ex) {
			}
			Context.destroy();
		}
	}
%>
<center>
<form method=post action="">
<textarea name="sql" rows="8" cols="66"></textarea>
<br>
<input type="submit" name="submit" value="submit">
</form>
</center>